package com.xujie.property_warranty_system.controller;

import com.xujie.property_warranty_system.common.Result;
import com.xujie.property_warranty_system.dto.MaintenanceDTO;
import com.xujie.property_warranty_system.dto.PageResult;
import com.xujie.property_warranty_system.entity.FacilityMaintenance;
import com.xujie.property_warranty_system.service.MaintenanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 设备维护记录控制器
 */
@RestController
@RequestMapping("/api/maintenance")
@CrossOrigin(origins = "*")
public class MaintenanceController {
    
    @Autowired
    private MaintenanceService maintenanceService;
    
    /**
     * 分页查询设备维护记录
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE')")
    public Result<PageResult<FacilityMaintenance>> getMaintenancePage(
            @RequestParam Long facilityId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            PageResult<FacilityMaintenance> result = maintenanceService.getMaintenancePage(facilityId, page, size);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取设备维护记录详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE')")
    public Result<FacilityMaintenance> getMaintenanceDetail(@PathVariable Long id) {
        try {
            FacilityMaintenance maintenance = maintenanceService.getMaintenanceDetail(id);
            return Result.success(maintenance);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 添加设备维护记录
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE')")
    public Result<Long> addMaintenance(@Valid @RequestBody MaintenanceDTO maintenanceDTO) {
        try {
            Long id = maintenanceService.addMaintenance(maintenanceDTO);
            return Result.success(id);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新设备维护记录
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE')")
    public Result<String> updateMaintenance(@PathVariable Long id, @Valid @RequestBody MaintenanceDTO maintenanceDTO) {
        try {
            maintenanceDTO.setId(id);
            maintenanceService.updateMaintenance(maintenanceDTO);
            return Result.success("维护记录更新成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除设备维护记录
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Result<String> deleteMaintenance(@PathVariable Long id) {
        try {
            maintenanceService.deleteMaintenance(id);
            return Result.success("维护记录删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取设备最近的维护记录
     */
    @GetMapping("/recent")
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE')")
    public Result<List<FacilityMaintenance>> getRecentMaintenanceRecords(
            @RequestParam Long facilityId,
            @RequestParam(defaultValue = "5") Integer limit) {
        try {
            List<FacilityMaintenance> records = maintenanceService.getRecentMaintenanceRecords(facilityId, limit);
            return Result.success(records);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
